package com.sinaapp.gavinzhang.GSpider;

import com.sinaapp.gavinzhang.GSpider.core.AbstractGWebUrlList;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * 爬虫系统自带的带访问列表
 * 实现采用阻塞性双头队列
 * @author gavin
 */
public class PendingWebUrlList extends AbstractGWebUrlList {

	/** 使用阻塞型双头队列*/
	private BlockingDeque<String> pendingList;
	public PendingWebUrlList() {
		pendingList = new LinkedBlockingDeque<String>();
	}
	@Override
	public boolean exist(String webUrl) {
		return pendingList.contains(webUrl);
	}

	@Override
	public String get() {
		try {
			return pendingList.take();
		} catch (InterruptedException e) {
			return null;
		}
	}

	@Override
	public AbstractGWebUrlList add(String webUrl) {
		if(pendingList.contains(webUrl)) {
			return this;
		}
		else {
			try {
				pendingList.put(webUrl);
			} catch (InterruptedException e) {
				System.err.println("CurrentWebUrlList function add error");
				e.printStackTrace();
			}
			return this;
		}
	}

	@Override
	public AbstractGWebUrlList remove(String webUrl) {
		if(pendingList.contains(webUrl)) {
			pendingList.remove(webUrl);
			return this;
		} else {
			return this;
		}
	}

	@Override
	public int size() {
		return pendingList.size();
	}
	@Deprecated
	public BlockingDeque<String> getBdCurrentList() {
		return pendingList;
	}
	@Deprecated
	public void setBdCurrentList(BlockingDeque<String> bdCurrentList) {
		this.pendingList = bdCurrentList;
	}

	public BlockingDeque<String> getPendingList() {
		return pendingList;
	}

	public void setPendingList(BlockingDeque<String> pendingList) {
		this.pendingList = pendingList;
	}
}
